// pages/page/sheping/resource_index/resource_index.js
var request_api = require('../../../../utils/require_api.js');
let timeId = null;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchResultDatas: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //点击查询资源的时候触发
  search_resources(event) {
    var that = this
    if (event.detail.value==''){
      wx.showToast({
        title: '搜索条件不能为空...',
        icon:'none'
      })
      return;
    }
    this.get_resource_class(event.detail.value);
  },

  get_resource_class(value){
    var that = this;
    request_api.request({
      "cmd": "weixin_api|get_wx_resources",
      "paras": [
        { name: "comdition", value: value },
        { name: "current_company_key", value: wx.getStorageSync("company_key") }//现在正式库的还没改脚本
      ]
    }, //需要传的 data
      function (res) {
        that.data.image_url = res.data.result;
       
        for (let i = 0; i <that.data.image_url.length;i++){
          if (that.data.image_url[i].resources_type==2){
            that.data.image_url[i].poster = that.data.image_url[i].url + wx.getStorageSync('video_poster_AVI')
          }else{
            that.data.image_url[i].poster = '';
          }
        }
        console.log(res.data.result, "this is result data ");
        that.setData({
          image_url: that.data.image_url,
          searchResultDatas: [],
          value
        })
      },
      function () {
        console.log("错误");
      },
    )
  },


  //资源页面查询的时候触发
  searchInput(e) {
    var that = this;
    timeId && clearTimeout(timeId);
    timeId = setTimeout(() => {
      request_api.request({
        "cmd": 'weixin_api|get_product_comdition',
        "paras": [
          { name: "comdition", value: e.detail.value }
        ]
      }, //需要传的 data
        function (res) {
          console.log(res,"this is searchInput function at resource_index.js 109");
          that.setData({
            searchResultDatas: res.data.result,
          })
        },
        function () {
          console.log("错误");
        },
      )
    }, 800)
  },
  //跳转到资源详情页面
  resources_detail(e) {
    wx.navigateTo({
      url: '/pages/page/sheping/resource_detail/resource_detail?rkey=' + e.currentTarget.dataset.rkey + '&resource_type=' + e.currentTarget.dataset.type,
    })
  },

  //点击关键词的时候模糊搜索查询
  itemclick(e){
    this.get_resource_class(e.currentTarget.dataset.condition);
  },
  //点击跳转到上传页面
  shangchuangbindtap(event) {
    wx.navigateTo({
      url: '/pages/page/sheping/uploadPage/uploadPage',
    })
  },
  //点击图片触发
  onTapImage() {

  },

  //清除搜索内容
  clearInput() {
    this.setData({
      value: '',
      searchResultDatas:[]
    })
  },

  goUser() {
    wx.redirectTo({
      url: '/pages/page/home/home',
    })
  }
})